﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Drawing;
using Gencode.PrsCommon.App;

namespace Gencode.PrsCommon.ControlData.GridControl
{
	/// <summary>
	/// PlanGridGenerator is used in PrsWeb
	/// This class can't be removed before PrsWeb is not changed
	/// </summary>
	public class PlanDienstGrid
	{
		public DataTable Table
		{
			get;
			set;
		}

		/// <summary>
		/// Adds single Item to Row
		/// </summary>
		/// <param name="value"></param>
		/// <param name="index"></param>
		/// <param name="columncount"></param>
		public void AddItem(string value, int column, int row , int columncount)
		{
			if (Table == null)
			{
				InitTable(columncount);
				DataRow dr = Table.NewRow();
				Table.Rows.Add(dr);
			}

			if (row < Table.Rows.Count)
				Table.Rows[row][column] = value;
		}

		/// <summary>
		/// Adds a empty row
		/// </summary>
		/// <param name="columncount">Amount of columns</param>
		public void AddRow(int columncount)
		{
			if (Table == null)
				InitTable(columncount);

			DataRow row = Table.NewRow();
			Table.Rows.Add(row);
		}

        /// <summary>
        /// Adds a Row with Data
        /// </summary>
        /// <param name="dv">DataView with data</param>
        /// <param name="columncount">Amount of columns</param>
		internal void AddRow(DataView dv, int columncount)
		{
			AddRow(columncount);

			if (dv.Count > 0)
			{
				int rowIndex = Table.Rows.Count -1;
				int day = 0;

				for (int d = 1; d <= columncount; d++)
				{
					if (dv.Count > 0)
					{
						day = int.Parse(DateUtil.GetDay(DateUtil.ConvertSqlToDate(dv[0].Row[0].ToString())));
					}
					if (day == d)
					{
						AddItem(dv[0].Row[1].ToString(), d - 1, rowIndex, columncount);
						dv.Delete(0);
					}
					else
						AddItem("", d - 1, rowIndex, columncount);
					day = 0;

				}
			}
		}

		public void InitTable(int columncount)
		{
			Table = new DataTable();
			for (int i = 0; i < columncount; i++)
			{
				Table.Columns.Add((i + 1).ToString(), typeof(string));
			}
		}
    }
}
